package mobi.greenplate.theweb.repository;

import mobi.greenplate.theweb.domain.UserDetail;
import java.util.List;
import java.util.ArrayList;
import javax.annotation.security.RolesAllowed;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import mobi.greenplate.theweb.domain.User;
import mobi.greenplate.theweb.user.UserUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository("userDetailRepository")
public class UserDetailRepository {

    private EntityManager entityManager;

    @PersistenceContext
    void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public void delete(Long id) {
        final UserDetail forDelete = entityManager.getReference(UserDetail.class, id);
        entityManager.remove(forDelete);
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public UserDetail save(UserDetail userDetail,User owner) {
        userDetail.setOwner(owner);
        return entityManager.merge(userDetail);
    }
    
    @RolesAllowed(value = {"ROLE_USER"})
    public UserDetail findById(Long id) {
        final UserDetail userDetail = entityManager.find(UserDetail.class, id);
        if(null==userDetail){
            throw new IllegalArgumentException("userDetail with id="+id+" not found");
        }
        return userDetail;        
    }
    
    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<UserDetail> findMyAll(){
        final List<UserDetail> founded = entityManager.createQuery("SELECT o FROM UserDetail o WHERE o.ownerId=:ownerId").
                setParameter("ownerId", UserUtils.getUserId()).getResultList();
        return new ArrayList<UserDetail>(founded);
    }

    public int countAll() {
        return ((Integer) entityManager.createQuery("SELECT count(o) from UserDetail o").
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<UserDetail> findAll(int firstResult, int maxResult) {
        final List<UserDetail> founded = entityManager.createQuery("SELECT o FROM UserDetail o").
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<UserDetail>(founded);
    }
    

    //ManyToOne by owner
    public int countByOwner(Long ownerId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from UserDetail o WHERE o.ownerId=:ownerId").
                setParameter("ownerId", ownerId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<UserDetail> findByOwner(int firstResult, int maxResult, Long ownerId) {
        final List<UserDetail> founded = entityManager.createQuery("SELECT o FROM UserDetail o WHERE o.ownerId=:ownerId").
                setParameter("ownerId", ownerId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<UserDetail>(founded);
    }    


    
    
}
